package com.emergency.controller;

import com.emergency.annotion.PhoneNumber;
import com.emergency.data.param.TestParam;
import com.emergency.http.DefaultResponseCode;
import com.emergency.http.MapWrapMapper;
import com.emergency.http.WrapMapper;
import com.emergency.http.Wrapper;
import com.emergency.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 测试数据
 *
 * @author makejava
 * @date 2020-09-18
 */
@RestController
@Validated
@RequestMapping("/test")
@Api(tags = "测试API")
@Slf4j
public class TestController extends BaseController {

    @Resource
    private TestService testService;

    /**
     * 模拟插入数据
     *
     * @param param 接收参数
     * @return Wrapper
     */
    @ApiOperation("插入数据")
    @PostMapping("/insert")
    public Wrapper<?> insert(@Valid TestParam param) {
        return WrapMapper.wrap(testService.insert(param)
                ? DefaultResponseCode.SAVE_SUCCESS : DefaultResponseCode.SAVE_FAIL);
    }

    /**
     * 模拟获取数据
     *
     * @return Wrapper
     */
    @ApiOperation("获取数据")
    @PostMapping("/getData")
    public Wrapper<?> getData() {
        reput("testList", testService.getData());
        return MapWrapMapper.map();
    }

    /**
     * 自定义注解校验手机号测试
     *
     * @return Wrapper
     */
    @ApiOperation("自定义注解校验手机号测试")
    @PostMapping("/testPhone")
    public Wrapper<?> testPhone(@ApiParam("手机号") @RequestParam @NotBlank(message = "phone 不能为空") @PhoneNumber String phone) {
        return WrapMapper.ok(phone);
    }

    @GetMapping(value = "/showPic")
    public void showPic(HttpServletResponse response) {
        try {
            OutputStream out = response.getOutputStream();
            response.setStatus(HttpServletResponse.SC_OK);

            //得到图片的二进制数据，以二进制封装得到数据，具有通用性
            byte[] bytes = readInputStream(new FileInputStream(ResourceUtils.getFile("classpath:confirm_ok.png")));

            out.write(bytes);
            out.close();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    private byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }
}

